import * as types from './mutation-types';
import {saveSearch, deleteSearch, clearSearch,savePlay,saveFavorite,deleteFavorite} from "common/js/cache"; // locastorage的封装
import {playMode} from 'common/js/config'
import {shuffle} from 'common/js/util'

// 异步改变state的数据
// 或者是在一次动作张改变多个mutation的话，建议使用actions

// 定义一个动作(选择播放的时候)执行，并提交以下的mutations，来更改state的状态
export const selectPlay = function ({commit, state}, {list, index}) {
  commit(types.SET_SEQUENCE_LIST, list); // 顺序播放
  commit(types.SET_PLAYLIST, list); // 播放列表
  commit(types.SET_CURRENT_INDEX, index); // 当前播放的index
  commit(types.SET_FULL_SCREEN, true); // 是否展开全屏播放
  commit(types.SET_PLAYING_STATE, true); // 是否开始播放
};

// 搜索的时候，点击歌曲，插入歌曲播放

// 查找列表中是否存在这首歌曲，存在就返回index
function findIndex(list, song) {
  return list.findIndex((item) => {
    return item.id === song.id
  })
}

export const insertSong = function ({commit, state}, song) {
  let playlist = state.playlist.slice();
  let sequenceList = state.sequenceList.slice();
  let currentIndex = state.currentIndex;
  // 记录当前歌曲
  let currentSong = playlist[currentIndex];

  // 查找列表中是否存在该歌曲返回index
  // 查找当前列表中是否有待插入的歌曲并返回其索引
  let fpIndex = findIndex(playlist, song);

  // 因为插入歌曲,所以索引要+1
  currentIndex++;
  // 插入这首歌曲到当前索引位置
  playlist.splice(currentIndex, 0, song);

  // 判断如果已经包含了这首歌曲
  if (fpIndex > -1) {
    // 如果当前插入的序号大于列表中的序号
    if (currentIndex > fpIndex) {
      playlist.splice(fpIndex, 1);
      currentIndex--;
    } else {
      playlist.splice(fpIndex + 1, 1);
    }
  }
  let currentSIndex = findIndex(sequenceList, currentSong) + 1;

  let fsIndex = findIndex(sequenceList, song);
  sequenceList.splice(currentSIndex, 0, song);

  if (fsIndex > -1) {
    if (currentSIndex > fsIndex) {
      sequenceList.splice(fsIndex, 1)
    } else {
      sequenceList.splice(fsIndex + 1, 1)
    }
  }
  // 提交mutations
  commit(types.SET_PLAYLIST, playlist);
  commit(types.SET_SEQUENCE_LIST, sequenceList);
  commit(types.SET_CURRENT_INDEX, currentIndex);
  commit(types.SET_FULL_SCREEN, true);
  commit(types.SET_PLAYING_STATE, true);
};
// 随机全部播放
export const randomPlay = function ({commit}, {list}) {
  commit(types.SET_PLAY_MODE, playMode.random);
  commit(types.SET_SEQUENCE_LIST, list);
  let randomList = shuffle(list);
  commit(types.SET_PLAYLIST, randomList);
  commit(types.SET_CURRENT_INDEX, 0);
  commit(types.SET_FULL_SCREEN, true);
  commit(types.SET_PLAYING_STATE, true);
};
// 保持历史搜索记录的动作
export const saveSearchHistory = function ({commit}, query) {
  commit(types.SET_SEARCH_HISTORY, saveSearch(query))
};
// 删除历史记录
export const deleteSearchHistory = function ({commit}, query) {
  commit(types.SET_SEARCH_HISTORY, deleteSearch(query))
};
// 删除所有的历史记录
export const clearSearchHistory = function ({commit}) {
  commit(types.SET_SEARCH_HISTORY, clearSearch())
};
// 删除歌曲
export const deleteSong = function ({commit, state}, song) {
  let playlist = state.playlist.slice(); // 播放列表
  let sequenceList = state.sequenceList.slice(); // 顺序列表
  let currentIndex = state.currentIndex; // 当前播放歌曲的index

  let pIndex = findIndex(playlist, song); // 找个这首歌曲在播放列表中的索引
  // 在播放列表中删除这首歌曲
  playlist.splice(pIndex, 1);
  let sIndex = findIndex(sequenceList,song); // 找个这首歌曲在顺序列表中的索引
  sequenceList.splice(sIndex,1);// 在顺序列表中删除这首歌曲

  // 如果删除的歌曲是在当前播放歌曲的索引 之后的话，就--currentIndex
  // 如果删除的是最后一首歌曲就--currentIndex
  if (currentIndex > pIndex || currentIndex === playlist.length) {
    currentIndex--
  }
  // 重新设置提交一下
  commit(types.SET_PLAYLIST,playlist);
  commit(types.SET_SEQUENCE_LIST,sequenceList);
  commit(types.SET_CURRENT_INDEX,currentIndex);

  // 如果整个播放列表都被删除了。就停止播放
  const playingState = playlist.length > 0;
  commit(types.SET_PLAYING_STATE,playingState)
};
// 清空播放列表
export const deleteSongList = function ({commit}) {
  commit(types.SET_PLAYLIST,[]);
  commit(types.SET_SEQUENCE_LIST,[]);
  commit(types.SET_CURRENT_INDEX,-1);
  commit(types.SET_PLAYING_STATE,false);
};
// 记录播放历史记录
export const savePlayHistory = function ({commit},song) {
  commit(types.SET_PLAY_HISTORY,savePlay(song))
};

// 收藏列表
export const saveFavoriteList = function ({commit},song) {
  commit(types.SET_FAVORITE_LIST,saveFavorite(song))
};
// 取消收藏
export const deleteFavoriteList = function ({commit},song) {
commit(types.SET_FAVORITE_LIST,deleteFavorite(song))
};
